# This file is part of Refix.
# 
# Refix is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
# 
# Refix is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
# 
# You should have received a copy of the GNU Lesser General Public
# License along with Refix.  If not, see <http://www.gnu.org/licenses/>.
# 
# Copyright (C) 2010-11 David Musgrove and others.

require 'albacore/support/albacore_helper'

class Refix 
  extend AttrMethods
  include RunCommand
  include YAMLConfig
  include Logging
  
  attr_accessor :command, :solution_file, :verbosity
  attr_array :options, :arguments
  
  def initialize
    super()
  end
  
  def run
    command_parameters = []
    command_parameters << "#{@command}"
    command_parameters << build_options unless @options.nil?
    command_parameters << "--verbose" if "#{@verbosity}" == "verbose"
    command_parameters << "--quiet" if "#{@verbosity}" == "quiet"
    command_parameters << "\"#{@solution_file}\"" unless @solution_file.nil?
    command_parameters << build_arguments unless @arguments.nil?
    
    result = run_command "Refix", command_parameters.join(" ")
    
    failure_msg = 'Refix task Failed. See Build Log For Detail.'
    fail_with_message failure_msg if !result
  end

  private 

  def build_options
     @options.join " "
  end
  
  def build_arguments
     @arguments.join " "
  end
end
